package com.crlui.effect
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	
	/**
	 * 乳摇效果
	 * @author crl
	 * 
	 */	
	public class BoobEffect extends Sprite
	{
		private var factory:TrianglesFac;
		public var size:int=130;
		public function BoobEffect(url:String)
		{
			super();
			
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
			loader.load(new URLRequest(url));
		}
		
		private function completeHandler(event:Event):void{
			var loaderInfo:LoaderInfo=event.target as LoaderInfo;
			loaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
			
			var bmd:BitmapData=(loaderInfo.content as Bitmap).bitmapData;
			
			var se:int=(bmd.width/10);
			factory=new TrianglesFac(bmd,se);
			factory.draw(this.graphics);
			
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseHandle);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseHandle);
			
			
			stage.addEventListener(Event.RESIZE,resizeHandle);
			resizeHandle(null);
		}
		
		private function resizeHandle(event:Event):void{
			
			this.x=(stage.stageWidth-factory.width)>>1;
			this.y=(stage.stageHeight-factory.height)>>1;
		}
		
		private var sx:int;
		private function mouseHandle(event:MouseEvent):void{
			if(event.type==MouseEvent.MOUSE_DOWN){
				
				sx=factory.get(event.localX,event.localY);
				var toY:int=factory.defaultVertices[sx+1];
				effectTo(event.localX,event.localY-120,toY);
				
			}else{
				
			}
		}
		
		private var o:Object={};
		private function effectTo(mx:int,my:int,toY:int):void{
			o.mx=mx;
			o.my=my;
			o.toY=toY;
			o.vy=0;
			
			this.addEventListener(Event.ENTER_FRAME,enter);
		}
		
		private function enter(event:Event):void{
			
			var dy:Number=o.toY-o.my;
			
			var vy:Number=o.vy;
			vy+=dy*0.3;
			vy*=0.8;
			
			o.my+=vy;
			o.vy=vy;
			
			onEnter(o.mx,o.my);
			
			if(Math.abs(dy)<0.01){
				this.removeEventListener(Event.ENTER_FRAME,enter);
			}
			
		}
		
		private function onEnter(mx:int,my:int):void{
			var dx:Number;
			var dy:Number;
			var m:Number;
			var deg:Number;
			
			var maxD:int=size;
			
			var g_dx:int=factory.defaultVertices[sx]-mx;
			var g_dy:int=factory.defaultVertices[sx+1]-my;
			
			var len:int=factory.defaultVertices.length;
			
			for(var i:int=0;i<len;i+=2){
				dx=factory.defaultVertices[i]-factory.defaultVertices[sx];
				dy=factory.defaultVertices[i+1]-factory.defaultVertices[sx+1];
				
				m=Math.sqrt(dx*dx+dy*dy);
				
				if(m>maxD){
					factory.vertices[i]=factory.defaultVertices[i];
					factory.vertices[i+1]=factory.defaultVertices[i+1];
				}else{
					
					factory.vertices[i]=factory.defaultVertices[i]-g_dx*(maxD-m)/maxD;
					factory.vertices[i+1]=factory.defaultVertices[i+1]-g_dy*(maxD-m)/maxD;
				}
				
			}
			
			factory.draw(this.graphics);
		}
	}
}